Python 基礎概念


變數賦值

Note:使用變數前不須宣告變數

a=10 #將 10 賦值給 a 
b='Hello world' #將 'Hello world' 賦值給 b
a,b=10,'Hello world' #將 10, 'Hello world' 分別賦值給 a 和 b
c=d=3 #將 3 賦值給 c 和 d

常見變數型態

  1. int 整數、float 浮點數 e.g. 1, 1.1 ...
  2. str 字串 e.g. 'Hello', 'Python'...
  3. 布林 e.g. True, False
  4. 串列 list e.g.[1,2,3]
  5. 元組 tuple e.g.(1,2)
  6. 字典 dict

串列介紹

  • 預設第一位索引值 index 為 0
  • 取得 list 元素:list[index]
  • 取得 list 最後一個元素:list[-1]
  • 獲取 list 長度:len(list)
  • 範圍取值:a[start:end:interval]
    1.start:從哪個 index 開始
    2.end: 到哪個 index 結束(不包含 end 本身)
    3.interval:間隔為多少,interval 若沒寫預設為 1
a=[1,2,3,4,5,6]
print a[0]  #output:1 取得 list index 為 0 的元素
print a[-1]  #output:6 取得 list 最後一個元素
print len(a)  #output:6
print a[2:5:2]  #output:[3,5]

新增與刪除

  • 使用 append 增加元素到 list 中,會加在最後面
  • 使用 insert 將元素插入 list 中:a.insert(想要插入的 index, 想要插入的內容)
  • 使用 remove 移除 list 中元素:a.remove(想要刪除的元素內容)
  • 使用 pop 刪除元素,會刪除最後一個元素:a.pop()
a=[1,2,3]
a.append('Hello')
print a  #output:[1,2,3,'Hello']

 a.insert(2,'python')
 print a  #output:[1,2,'python',3,'Hello']

 a.remove('python')
 print a #output:[1,2,3,'Hello']

 a.pop()
 print a  #output:[1,2,3]

元組 tuple 介紹

  • 存取 tuple 元素:my_tuple[index]
  • tuple 不可改變,創建後就不能修改元素
  • unpacking tuple:my_tuple(1,2,3) a,b,c=my_tuple
  • packing tuple:my_tuple=1,2,3
  • 其他函數:len() 計算長度、max() 找最大值、min() 找最小值
#packing
my_tuple=1,2,3
print my_tuple  #output:(1,2,3)

#存取 tuple 元素
print my_tuple[0] #output:1

#unpacking
a,b,c=my_tuple
print a #output:1
print b #output:2
print c #output:3

字典 dict 介紹

  • 由 value 和 key 組成:my_dict{key:value}
  • 用 key 取值:my_dict[key]
  • 指定 key 修改字典內容:my_dict[想要修改的 key]=想要修改成的內容
  • 新增 key 和 value:my_dict[想要新增的 key]=想要新增的 value
  • 刪除字典內容:del my_dict[key]
  • 取得所有 key 和所有 value:keys=my_dict.keys()、values=my_dict.values()
  • 取得所有 key 和 value (一起):items=my_dict.items()
my_dict={'1':'a',2:'b'}
#用 key 取值
print my_dict['1']  #output:'a'

#指定 key 修改字典內容
my_dict[2]='c'
print my_dict  #output:my_dict{'1':'a',2:'c'}

#新增 key 和 value
my_dict['Tue']='python'
print my_dict #output:my_dict{'1':'a',2:'c','Tue':'python'}

#刪除字典內容
del my_dict['Tue'] 
print my_dict #output:my_dict{'1':'a',2:'c'}

條件與迴圈

條件語句結構

if-elif(相當於 C++ 中 else if 的用法)-else
!!!冒號下一行記得空格,否則會有 error!!!

if num=3:
    print num
elif num=5:
    print num
else:
    print 'else'

迴圈

for 或 while,以下用判斷奇數偶數為例子

number_list=[1,2,3,5,67,34,2]
for number in number_list:
    if number%2==0:
        print number,' is even.'
    else:
        print number,' is odd.'
number_list=[1,2,3,5,67,34,2]
count=len(number_list)
while count>0:
    number=number_list.pop()
    if number%2==0:
        print number,' is even.'
    else:
        print number,' is odd.'
    count=count-1

定義函數

def even_or_odd(number):
    if type(number) == int:  
        if number % 2 == 0:
            print number,' is even.' 
        else:
            print number,' is odd.' 
    else:
        print('Please enter an integer.')

even_or_odd(10) #output:10  is even.
even_or_odd('10') #output:Please enter an integer.

import

用來引入所需要的模組 e.g. import openai
from 模組名稱 import 所需的部分模組

Note:有些模組需要先下載才能引入,以下提供一個下載方法
開啟「命令提示字元(cmd)」->使用 pip install 後面加上要下載的模組 -> 安裝完成即可

檔案操作os

參考資料:https://steam.oxxostudio.tw/category/python/library/os.html


資料來源

菜鳥教程 Python 基礎教程: https://www.runoob.com/python/python-tutorial.html
Python 基礎操作與常見資料型態: https://ithelp.ithome.com.tw/m/articles/10265829
Medium 相關文章: https://medium.com/@virginiakm1988


----------------------------進階語法------------------------------
常見數據分析模組:

  1. matplotlib
  2. pandas
  3. sklearn
  4. numpy
  5. seaborn
  6. tensorflow
  7. keras
  8. scipy
#Python






你可能感興趣的文章

1338. Reduce Array Size to The Half

1338. Reduce Array Size to The Half

550. Game Play Analysis IV

550. Game Play Analysis IV

Simple Twitter 專案開發心得

Simple Twitter 專案開發心得






留言討論